package net.lab1024.sa.base.module.support.rule.controller;

import net.lab1024.sa.base.common.code.ErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import net.lab1024.sa.base.module.support.rule.domain.entity.CepEntity;
import net.lab1024.sa.base.module.support.rule.domain.form.CepAddForm;
import net.lab1024.sa.base.module.support.rule.domain.form.CepQueryForm;
import net.lab1024.sa.base.module.support.rule.domain.form.CepUpdateForm;
import net.lab1024.sa.base.module.support.rule.domain.vo.CepVO;
import net.lab1024.sa.base.module.support.rule.service.CepService;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.mvel2.MVEL;
import org.mvel2.ParserContext;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 规则主表 Controller
 *
 * @Author chenyuqiang
 * @Date 2025-08-21 22:46:37
 * @Copyright 1
 */

@RestController
@Tag(name = "规则主表")
@OperateLog
public class CepController {

    @Resource
    private CepService cepService;

    @Operation(summary = "分页查询 @author chenyuqiang")
    @PostMapping("/cep/queryPage")
    @SaCheckPermission("cep:query")
    public ResponseDTO<PageResult<CepVO>> queryPage(@RequestBody @Valid CepQueryForm queryForm) {
        return ResponseDTO.ok(cepService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author chenyuqiang")
    @PostMapping("/cep/add")
    @SaCheckPermission("cep:add")
    public ResponseDTO<String> add(@RequestBody @Valid CepEntity addForm) {
        return cepService.add(addForm);
    }

    @Operation(summary = "添加 @author chenyuqiang")
    @GetMapping("/cep/detail/{id}")
    public ResponseDTO<CepEntity> detail(@PathVariable("id") String id) {
        return cepService.detail(id);
    }

    @Operation(summary = "更新 @author chenyuqiang")
    @PostMapping("/cep/update")
    @SaCheckPermission("cep:update")
    public ResponseDTO<String> update(@RequestBody @Valid CepEntity updateForm) {
        return cepService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author chenyuqiang")
    @PostMapping("/cep/batchDelete")
    @SaCheckPermission("cep:delete")
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Long> idList) {
        return cepService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author chenyuqiang")
    @GetMapping("/cep/delete/{id}")
    @SaCheckPermission("cep:delete")
    public ResponseDTO<String> batchDelete(@PathVariable Long id) {
        return cepService.delete(id);
    }

    @Operation(summary = "验证EPL语法 @author chenyuqiang")
    @PostMapping("/business/cep/validate-syntax")
    public ResponseDTO<String> validateEplSyntax(@RequestBody String epl) {
        try {
            // 使用 MvelValidator 进行验证
            MvelValidator.ValidationResult validationResult = MvelValidator.validateWithDetailsNew(epl);
            if (validationResult.isValid()) {
                return ResponseDTO.ok(validationResult.toString());
            } else {
                return ResponseDTO.errorMsg(validationResult.toString());
            }
        } catch (Exception e) {
            // 返回具体的错误信息
            return ResponseDTO.errorMsg("验证过程中发生错误: " + e.getMessage());
        }
    }
}
